package com.hebeiwangxiaozhang.admin.repo;

import com.hebeiwangxiaozhang.admin.pojo.CommodityBrand;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import javax.persistence.QueryHint;
import java.util.List;
import java.util.Optional;

@Repository
public interface CommodityBrandRepository extends JpaRepository<CommodityBrand, Long> {

    @Override
    @Lock(LockModeType.PESSIMISTIC_READ)
    @QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value = "3000")})
    Optional<CommodityBrand> findById(Long aLong);

    @Query(value = "SELECT b.* FROM commodity_brand b LEFT " +
            "JOIN commodity_category_brand cb ON b.id = cb.brand_id WHERE cb.category_id " +
            "= :categoryId", nativeQuery = true)
    List<CommodityBrand> getByCategoryId(Long categoryId);

    List<CommodityBrand> getByLetter(String brandName);

}
